define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'general/screenitem/index',
                    add_url: 'general/screenitem/add',
                    edit_url: 'general/screenitem/edit',
                    del_url: 'general/screenitem/del',
                    multi_url: 'general/screenitem/multi',
                    tagtype_url: "general/screenitem/getdynamic",
                    table: 'screens_items',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName:'id',
                //searchFormVisible: true,
                //searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'),placeholder: __('模糊查询'),operate: 'LIKE %...%',},
                        {field: 'screens_id', title: __('屏幕名称'),addclass:'disabled',searchList:screenList,formatter:function(value,row,index){
                            if(row.screens==null){
                                return "-";
                            }else{
                                return row.screens.name;
                            }
                        }},
                        {field: 'region.name',title: __('局点'),operate: 'LIKE %...%',formatter:function(value,row,index){
                            if(value){
                                return row.region.name;
                            }else{
                                return "-";
                            }
                        }},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type0'),"1":__('Type1')},formatter:function(data,full){
                                if(data == "0"){
                                    return  __('Type0');
                                }else{
                                    return  __('Type1');
                                }
                        }},
                        /*{field: 'tag_type_id', title: __('标签类型'),visible:false,operate:false,searchList:tagtypeList,formatter:function(value,row,index){
                            if(row.tagtype==null){
                                return "-";
                            }else{
                                return row.tagtype.name;
                            }
                            
                        }},
                        {field: 'tag_id', title: __('标签名称'),visible:false,operate:false,searchList:tagList,formatter:function(value,row,index){
                            if(row.tag==null||row.tagtype==null){
                                return "-";
                            }else{
                                return row.tag.name;
                            }
                        }},
                        {field: 'device_id', title: __('Device_id'),visible:false,operate:false,searchList:deviceList,formatter:function(value,row,index){
                            if(row.device==null){
                                return "-";
                            }else{
                                return row.device.ip;
                            }
                        }},
                        {field: 'dynamic', title: __('Dynamic'),visible:false,placeholder: __('模糊查询'),operate: 'LIKE %...%'},*/
                        {field: 'private', title: __('展示'),align: 'center',formatter: Table.api.formatter.toggle, searchList: {"1":__('展示'),"0":__('不展示')}},
                        {field: 'weighs', title: __('权重')},
                        {field: 'ipifindex', title: __('ipifindex'),operate:false,visible:false},
                        {field: 'updatetime', title: __('Update time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        /*{field: 'width', title: __('Width'),operate:false},
                        {field: 'height', title: __('Height'),operate:false},
                        {field: 'colspan', title: __('Colspan'),operate:false},
                        {field: 'rowspan', title: __('Rowspan'),operate:false},*/
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $('select[name="tag_id"]').attr("disabled","disabled");
            $('select[name="tag_id"]').attr("title","请先选择标签类型");
            $('select[name="tag_type_id"]').on('change',function(){
                $('select[name="tag_id"]').removeAttr("disabled");
                var tagname_str = $(this).val();
                if(tagname_str!=null&&tagname_str!=""){
                    $.ajax({
                        type:"POST",
                        url: $.fn.bootstrapTable.defaults.extend.tagtype_url,
                        data:{type:$(this).val()},
                        dataType:"json",
                        success:function(data){
                            $('select[name="tag_id"]').empty();
                            $('select[name="tag_id"]').append('<option value="" >' + '选择' + '</option>');
                            for(var item of data) {
                                $('select[name="tag_id"]').append('<option value="'+item.id + '" >' + item.name + '</option>');
                            };
                        }
                    });
                }else{
                    $('select[name="tag_id"]').attr("disabled","disabled");
                    $('select[name="tag_id"]').attr("title","请先选择标签类型");
                    $('select[name="tag_id"]').empty();
                    $('select[name="tag_id"]').append('<option value="" >' + '选择' + '</option>');
                }
            })
            
            // 为表格绑定事件
            Table.api.bindevent(table);
       
        },
        add: function () {
            $("#c-tag_type_id").data("params",function(obj){
                return {row:{tag_type_id:$("#type_id").val()}};
            });
            $("#c-device_id").data("params",function(obj){
                return {row:{device_id:$("#deviceid").val()}};
            });
            Controller.api.bindevent();
            //设备IP切换的时候  设备接口的值清空
            $("select[name='row[device_id]']").change(function(){
                $("#c-device_id").val("");
                $(".selected_tag").remove();
            });
            //标签类型切换的时候  标签名称的值清空
            $("select[name='row[tag_type_id]']").change(function(){
                $("#c-tag_type_id_text").val("");
                $("#c-tag_type_id").val("");
                $(".selected_tag").remove();
            });
            $('select[name="row[type]').on('change',function(){
                var type_str = $('select[name="row[type]"]').val();
                if(type_str==1){
                    document.getElementById("tagtypediv").style.display="";
                    document.getElementById("tagdiv").style.display="";
                    document.getElementById("regdiv").style.display="";
                    document.getElementById("devicediv").style.display="none";
                    document.getElementById("dynamicdiv").style.display="none";
                    /*var options = $('select[name="row[tag_type_id]"]').find("option");
                    options.first().attr("selected", true);
                    $(".selected_tag").empty();
                    $('select[name="row[tag_type_id]"]').val("");
                    $("#c-tag_type_id_text").val("");
                    $('select[name="row[tag_id]"]').val("");
                    $('select[name="row[tag_id]"]').attr("disabled","disabled");*/
                }else{
                    document.getElementById("tagtypediv").style.display="none";
                    document.getElementById("tagdiv").style.display="none";
                    document.getElementById("regdiv").style.display="none";
                    document.getElementById("devicediv").style.display="";
                    document.getElementById("dynamicdiv").style.display="";
                    /*$('.selectpicker').selectpicker('refresh');
                    var options = $('select[name="row[device_id]"]').find("option");
                    options.first().attr("selected", true);
                    $(".selected_tag").empty();
                    $("#c-device_id").val("");
                    $('select[name="row[dynamic]"]').val("");
                    $('select[name="row[dynamic]"]').attr("disabled","disabled");*/
                }
            });
            //切换屏幕名称的时候，对应的局点跟着切换
            $(document).on("change","#c-screens_id",function(){
                var screens_id = $('select[name="row[screens_id]"]').val();
                var screen_tag = Config.screen_tag;
                $("#item_screen_tag").val(screen_tag[screens_id]);
            });
            
        },
        edit: function () {
            $("#c-tag_type_id").data("params",function(obj){
                return {row:{tag_type_id:$("#type_id").val()}};
            });
            $("#c-device_id").data("params",function(obj){
                return {row:{device_id:$("#deviceid").val()}};
            });
            Controller.api.bindevent();
            $('select[name="row[type]"]').attr("disabled","disabled");
            //设备IP切换的时候  设备接口的值清空
            $("select[name='row[device_id]']").change(function(){
                $("#c-device_id").val("");
                $(".selected_tag").remove();
            });
            //标签类型切换的时候  标签名称的值清空
            $("select[name='row[tag_type_id]']").change(function(){
                $("#c-tag_type_id").val("");
                $("#c-tag_type_id_text").val("");
                $(".selected_tag").remove();
            });
            //切换屏幕名称的时候，对应的局点跟着切换
            $(document).on("change","#c-screens_id",function(){
                var screens_id = $('select[name="row[screens_id]"]').val();
                var screen_tag = Config.screen_tag;
                $("#item_screen_tag").val(screen_tag[screens_id]);
            });
            $('select[name="row[type]').on('change',function(){
                var type_str = $('select[name="row[type]"]').val();
                if(type_str==1){
                    document.getElementById("tagtypediv").style.display="";
                    document.getElementById("tagdiv").style.display="";
                    document.getElementById("regdiv").style.display="";
                    document.getElementById("devicediv").style.display="none";
                    document.getElementById("dynamicdiv").style.display="none";
                    /*$(".selected_tag").remove();
                    var options = $('select[name="row[tag_type_id]"]').find("option");
                    options.first().attr("selected", true);
                    $("#c-region_id_text").val("");
                    $("#c-tag_type_id_text").val("");*/
                    //$('select[name="row[tag_id]"]').val("");
                    //$('select[name="row[tag_id]"]').attr("disabled","disabled");
                }else{
                    document.getElementById("tagtypediv").style.display="none";
                    document.getElementById("tagdiv").style.display="none";
                    document.getElementById("regdiv").style.display="none";
                    document.getElementById("devicediv").style.display="";
                    document.getElementById("dynamicdiv").style.display="";
                    //$(".selected_tag").remove();
                    //$("#c-device_id").val("");
                    /*var options = $('select[name="row[device_id]"]').find("option");
                    options.first().attr("selected", true);*/
                    //$('select[name="row[dynamic]"]').val("");
                    //$('select[name="row[dynamic]"]').attr("disabled","disabled");
                }
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});